package com.nie.shoppdevstudy.controller;

import com.nie.shoppdevstudy.bean.User;
import com.nie.shoppdevstudy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
public class LoginController {
    @Autowired
    UserService service;
    //访问登录页面
    @RequestMapping("/login")
    public String getLogin(){
        return "login";
    }
    //访问注册页面
    @RequestMapping("/register.html")
    public String getRegister(){
        return "register";
    }
    //访问主页
    @PostMapping("/login")
    public String getIndex(User user, HttpSession session,HttpServletRequest request){
        User loginUser = service.getUserByName(user.getUserName());
        if("".equals(user.getUserName())){
            request.setAttribute("error","请输入用户名");
            return "login";
        }
        if("".equals(user.getPassword())){
            request.setAttribute("error","请输入密码");
            return "login";
        }
        if(loginUser!=null&&user.getUserName().equals(loginUser.getUserName())&&user.getPassword().equals(loginUser.getPassword())){
            session.setAttribute("user",user);
        return "redirect:index";
        }else {
            request.setAttribute("error","用户名不存在或密码错误,请重新输入");
            return "login";
        }
    }
    @PostMapping ("/register")
    public String getRegister(User user, Model model,HttpServletRequest request){
        User userName = service.getUserByName(user.getUserName());//通过用户名查询用户
        User phoneNumber = service.getUserByPhone(user.getPhoneNumber());//通过手机号查询用户
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
        Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(user.getPhoneNumber());
        if("".equals(request.getParameter("phoneNumber"))){
            model.addAttribute("error","请输入手机号");
            return "register";
        }
        if("".equals(request.getParameter("userName"))){
            model.addAttribute("error","请输入用户名");
            return "register";
        }
        if("".equals(request.getParameter("password"))){
            model.addAttribute("error","请输入密码");
            return "register";
        }
        if("".equals(request.getParameter("rPassword"))){
            model.addAttribute("error","请输入确认密码");
            return "register";
        }
        //正则表达式判断手机号码是否合法
        if(!m.matches()){
            model.addAttribute("error","请输入正确格式的手机号");
            return "register";
        }
        //检查用户名是否存在
        if(userName!=null){
            model.addAttribute("error","用户名已存在");
            return "register";
        }
        //检查手机号是否注册过
        if(phoneNumber!=null){
            model.addAttribute("error","该手机号已被注册");
            return "register";
        }
        if(!request.getParameter("rPassword").equals(user.getPassword())){
            model.addAttribute("error","两次密码不一致");
            return "register";
        }
        user.setRegisterTime(new Date(System.currentTimeMillis()));
        //注册成功，添加用户，返回登陆页面
        service.addUser(user);
        model.addAttribute("success","注册成功");
        return "redirect:login";

    }
    //访问首页
    @GetMapping("/index")
    public String getIndex(HttpSession session){
        Object user = session.getAttribute("user");
        if(user==null){
            return "login";
        }
        return "index";
    }


}
